package com.yjhb.android.sbz.adapter;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.Report;
import com.yjhb.android.sbz.enums.ReportState;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxTimeTool;
import com.jojo.android.zxlib.view.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建者 zx
 * 创建时间 2020/12/22 15:35
 */
public class TipOffAdapter extends BaseRecyclerAdapter<TipOffAdapter.TipOffHolder, Report> {
    private final String TAG = "TipOffAdapter";

    private boolean isLaunch;

    public TipOffAdapter(Context context, boolean isLaunch) {
        super(context);
        this.isLaunch = isLaunch;
    }

    @Override
    protected TipOffHolder getHolder(View v) {
        return new TipOffHolder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_report;
    }

    @Override
    public void onBindViewHolder(@NonNull TipOffHolder tipOffHolder, int i) {
        final int index = i;
        Report report = data.get(i);
        tipOffHolder.timeTv.setText("举报时间:" + ZxTimeTool.getDate(entity.getCreateTime().getTime(),
                "yyyy-MM-dd HH:mm"));
        ReportState state = ReportState.getState(entity.getState());
        tipOffHolder.stateTv.setText(state.getStateStr());
        //如果是我举报的就隐藏我要辩诉按钮
        tipOffHolder.plea.setVisibility(isLaunch ? View.GONE : View.VISIBLE);
        //别人举报我的，并且在等待辩诉状态才显示我要辩诉
        if (!isLaunch && state == ReportState.REPORT_STATE_WAIT_PLEA) {
            tipOffHolder.plea.setVisibility(View.VISIBLE);
            tipOffHolder.plea_tv.setText("我要辩诉");
        } else {
            tipOffHolder.plea.setVisibility(View.GONE);
        }
        int s = report.getSubmitAgainState();
        //双方均不能继续辩诉
        if (s == 0) {
            tipOffHolder.continue_ll.setVisibility(View.GONE);
        }
        //被举报者不能继续辩诉
        else if (s == 1 && !isLaunch) {
            tipOffHolder.continue_ll.setVisibility(View.GONE);
        }
        //举报者不能继续辩诉
        else if (s == 2 && isLaunch) {
            tipOffHolder.continue_ll.setVisibility(View.GONE);
        }
        //可以继续辩诉
        else {
            tipOffHolder.continue_ll.setVisibility(View.VISIBLE);
        }
        //如果是我举报的
        if (isLaunch) {
            Glide.with(context).load(entity.getDefendantAvatar())
                    .apply(new RequestOptions().placeholder(R.mipmap.test_head))
                    .into(tipOffHolder.headIv);
            tipOffHolder.nicknameTv.setText(entity.getDefendantName());
            tipOffHolder.srcTv.setText("来自会员:" + entity.getDefendant());
        } else {
            Glide.with(context).load(entity.getPlaintiffAvatar())
                    .apply(new RequestOptions().placeholder(R.mipmap.test_head))
                    .into(tipOffHolder.headIv);
            tipOffHolder.nicknameTv.setText(entity.getPlaintiffName());
            tipOffHolder.srcTv.setText("来自会员:" + entity.getPlaintiff());
        }
        //我举报的显示撤销举报按钮否则联系举报人
        tipOffHolder.button2_tv.setText(isLaunch ? "撤销举报" : "联系举报人");
        //只有在等待辩诉的状态下才显示撤销举报或者联系举报人
        if (ReportState.getState(entity.getState()) == ReportState.REPORT_STATE_WAIT_PLEA) {
            tipOffHolder.button2_tv.setVisibility(View.VISIBLE);
        } else {
            tipOffHolder.button2_tv.setVisibility(View.GONE);
        }

        tipOffHolder.lookOver_tv.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(v, "lookOver", index, report);
            }
        });
        tipOffHolder.plea.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onViewClick(v, tipOffHolder.plea_tv.getText().toString().equals("我要辩诉") ?
                        "plea" : "通过任务", index, report);
            }
        });
        tipOffHolder.button2_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onViewClick(v, isLaunch ? "撤销举报" : "联系举报人", index, report);
                }
            }
        });
        tipOffHolder.continue_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onViewClick(v, "continue", index, report);
                }
            }
        });


    }

    class TipOffHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.head_iv)
        CircleImageView headIv;
        @BindView(R.id.nickname_tv)
        TextView nicknameTv;
        @BindView(R.id.src_tv)
        TextView srcTv;
        @BindView(R.id.time_tv)
        TextView timeTv;
        @BindView(R.id.state_tv)
        TextView stateTv;
        @BindView(R.id.plea)
        LinearLayout plea;
        @BindView(R.id.lookOver_tv)
        TextView lookOver_tv;
        @BindView(R.id.button2_tv)
        TextView button2_tv;
        @BindView(R.id.plea_tv)
        TextView plea_tv;
        @BindView(R.id.continue_ll)
        LinearLayout continue_ll;

        public TipOffHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
